home *** CD-ROM | disk | FTP | other *** search
- Path: calvin.stemnet.nf.ca!jdeeley
- From: jdeeley@calvin.stemnet.nf.ca (J.Deeley)
- Newsgroups: comp.lang.c
- Subject: bubble sort walkthrough needed, please
- Date: Wed, 17 Apr 1996 14:26:02 -0500
- Organization: I need to be organized?
- Message-ID: <4l39b4$s2h@coranto.ucs.mun.ca>
- NNTP-Posting-Host: calvin.stemnet.nf.ca
- X-Newsreader: MacSOUP 1.0d7
-
- Hi, everyone. I would appreciate your help on this problem that I am
- sure you could code in your sleep.
-
- I am trying to understand how this bubble sort is working. I understand
- the theory, and I understand how the whole thing works except for the
- few lines under the comment /* now sort them using a bubble sort */
- In other words, it's only the lines
-
- for(a=1; a<count; a++)
- for(b=count-1; b>=a;--b)
-
- that I can't fathom. I've been working at this ever since last night and
- I still don't get it. Could someone tell me what is going on in that
- part?
-
-
-
- /*this program illustrates the bubble sort*/
-
- #include <stdio.h>
- #include <stdlib.h>
-
- main ()
-
- {
- int item[100];
- int a, b, t;
- int count;
-
- /* read in numbers */
- printf("How many numbers? ");
- scanf("%d", &count);
- for (a=0; a<count; a++) scanf("%d", &item[a]);
-
- /* now sort them using a bubble sort */
- for(a=1; a<count; a++)
- for(b=count-1; b>=a;--b) {
- /* compare the adjacent elements */
- if(item [b-1] > item [b]) {
- /* exchange elements */
- t = item [b-1];
- item[b-1] = item [b];
- item [b] = t;
- }
- }
-
- /*display sorted list*/
- for (t=0; t<count; t++) printf("%d", item [t]);
- }
-
-
- Thanks!
- JD
- ---------------------
- Caution...Newbie Programmer on Board!
- --
-
-